﻿/**
 *  Distributed as part of Free.iso8583
 *  
 *  Free.iso8583 is ISO 8583 Message Processor library that makes message parsing/compiling esier.
 *  It will convert ISO 8583 message to a model object and vice versa. So, the other parts of
 *  application will only do the rest effort to make business process done.
 *  
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License or (at your option) any later version. 
 *  See http://gnu.org/licenses/lgpl.html
 *
 *  Developed by AT Mulyana (atmulyana@yahoo.com) 2009-2010
 *  The latest update can be found at sourceforge.net
 **/
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Free.iso8583.model
{
    public class BaseModel
    {
        public BaseModel()
        {
        }

        public BaseModel(Object aModel)
        {
            CopyFrom(aModel);
        }

        public BaseModel CopyFrom(Object aModel)
        {
            if (this.GetType() != aModel.GetType() && !this.GetType().IsSubclassOf(aModel.GetType()))
            {
                throw new MessageProcessorException("Cannot copy model, incompatible type.");
            }

            PropertyInfo[] properties = aModel.GetType().GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (!property.CanRead || !property.CanWrite) continue;
                Object propVal = property.GetValue(aModel, null);
                property.SetValue(this, propVal, null);
            }

            return this;
        }
    }
}
